package cn.iocoder.yudao.module.jikong.service.jkProduct;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.jikong.dal.dataobject.project.JkProjectDO;
import cn.iocoder.yudao.module.jikong.dal.mysql.project.JkProjectMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.jikong.controller.admin.jkProduct.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.jkProduct.JkProductDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.jikong.dal.mysql.jkProduct.JkProductMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.jikong.enums.ErrorCodeConstants.*;

/**
 * 疾控产品表 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class JkProductServiceImpl implements JkProductService {

    @Resource
    private JkProductMapper jkProductMapper;
    @Resource
    private JkProjectMapper jkProjectMapper;

    @Override
    public Long createJkProduct(JkProductSaveReqVO createReqVO) {
        // 插入
        JkProductDO jkProduct = BeanUtils.toBean(createReqVO, JkProductDO.class);

        //根据projectId查询areaId
        JkProjectDO jkProject = jkProjectMapper.selectById(jkProduct.getProjectId());
        jkProduct.setAreaId(Math.toIntExact(jkProject.getAreaId()));

        jkProductMapper.insert(jkProduct);
        // 返回
        return jkProduct.getId();
    }

    @Override
    public void updateJkProduct(JkProductSaveReqVO updateReqVO) {
        // 校验存在
        validateJkProductExists(updateReqVO.getId());
        // 更新
        JkProductDO updateObj = BeanUtils.toBean(updateReqVO, JkProductDO.class);

        JkProjectDO jkProject = jkProjectMapper.selectById(updateObj.getProjectId());
        updateObj.setAreaId(Math.toIntExact(jkProject.getAreaId()));


        jkProductMapper.updateById(updateObj);
    }

    @Override
    public void deleteJkProduct(Long id) {
        // 校验存在
        validateJkProductExists(id);
        // 删除
        jkProductMapper.deleteById(id);
    }

    @Override
        public void deleteJkProductListByIds(List<Long> ids) {
        // 校验存在
        validateJkProductExists(ids);
        // 删除
        jkProductMapper.deleteByIds(ids);
        }

    private void validateJkProductExists(List<Long> ids) {
        List<JkProductDO> list = jkProductMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(JK_PRODUCT_NOT_EXISTS);
        }
    }

    private void validateJkProductExists(Long id) {
        if (jkProductMapper.selectById(id) == null) {
            throw exception(JK_PRODUCT_NOT_EXISTS);
        }
    }

    @Override
    public JkProductDO getJkProduct(Long id) {
        return jkProductMapper.selectById(id);
    }

    @Override
    public PageResult<JkProductDO> getJkProductPage(JkProductPageReqVO pageReqVO) {
        return jkProductMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateJkProductCount(Long id, Integer clickCount) {
        // 校验存在
        validateJkProductExists(id);
        // 更新
        JkProductDO updateObj = new JkProductDO();
        updateObj.setId(id);
        updateObj.setClickCount(clickCount);
        jkProductMapper.updateById(updateObj);

    }

    @Override
    public List<JkProductDO> getJkProductHot() {
        return jkProductMapper.selectHot();
    }

    @Override
    public List<JkProductDO> getJkProductRecommend() {
        //随机获取数据
        return jkProductMapper.selectRecommend();
    }

}
